/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Stones;

import ElementsProperties.Buyable;
import Model.CollisionEvent;
import java.awt.image.BufferedImage;

/**
 *
 * @author Snowangelic
 */
public class CrateDispenser extends AbstractSolidStone implements Buyable {

    private BufferedImage[] animation;

    private int currentAnimationImg;
    private int animImgsCount;

    private long timeByImg;
    private long currentAnimationTime;

    private boolean animationOn;

    public CrateDispenser(){
        currentAnimationImg=0;
        currentAnimationTime=0;
        animImgsCount=20;
        timeByImg=500/animImgsCount;
        animationOn=false;

        image=EnigmaUtils.ImageLoader.loadImage("./images/st-coinslot.png");
        animation=createAnimImgs("./images/st-coin2slot.png");
        System.out.println("Created");
    }

    public BufferedImage[] createAnimImgs(String filename){
         BufferedImage[] itemAnim = new BufferedImage[animImgsCount];
         BufferedImage imgTemp = EnigmaUtils.ImageLoader.loadImage(filename);
         for (int i=0;i<animImgsCount;i++){
             itemAnim[i] = imgTemp.getSubimage(0, i*32, 32, 32);
         }
         return itemAnim;
    }

    @Override
    public void update(long dtime) {
        if (animationOn){
            currentAnimationTime+=dtime;
            if (currentAnimationTime>=currentAnimationImg*timeByImg){
                currentAnimationImg++;
                this.changed();
            }
            if (currentAnimationImg<animImgsCount+1)
                image=animation[currentAnimationImg-1];//currentAnimationImg initialisé à 0 puis passe à 1 avec le IF precedent
            else
            {
                animationOn=false;
                currentAnimationImg=0;
                currentAnimationTime=0;
            }
        }
    }

    public boolean buy() {
        boolean bought=false;
        if(myCase.getCase(myCase.getXIndex()+2,myCase.getYIndex()) != null)
            if(myCase.getCase(myCase.getXIndex()+2,myCase.getYIndex()).getStone() == null){
                myCase.getCase(myCase.getXIndex()+2,myCase.getYIndex()).setStone(new SimpleCrate());
                animationOn=true;
                bought=true;
            }

        return bought;
    }
}
